package com.admin.service;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.admin.bean.BillingBean;
import com.admin.bean.BillingSpBean;
import com.admin.bean.ConsumeOrderBean;
import com.admin.bean.LogOrderBean;
import com.admin.bean.MobileUserBean;
import com.admin.bean.SpBean;
import com.admin.bean.TempOrderBean;
import com.admin.dao.BillingDao;
import com.admin.dao.BillingSpDao;
import com.admin.dao.ConsumeOrderDao;
import com.admin.dao.LogOrderDao;
import com.admin.dao.MobileUserDao;
import com.admin.dao.SpDao;
import com.admin.dao.TempOrderDao;

@Service
public class ConsumeOrderService {

	private static final Logger logger = Logger.getLogger(ConsumeOrderService.class);
	
	@Autowired private SpDao spDao;
	@Autowired private MobileUserDao mobileUserDao;
	@Autowired private BillingSpDao billingSpDao;
	@Autowired private BillingDao billingDao;
	@Autowired private ConsumeOrderDao consumeOrderDao;
	@Autowired private TempOrderDao tempOrderDao;
	@Autowired private LogOrderDao	logOrderDao;
	
	/**
	 * 添加订单
	 * @param telPhone
	 * @param port
	 * @param command
	 * @param consume
	 * @return
	 */
	public boolean insertOrder(String telPhone,String port,String command,int consume){
		try {
			LogOrderBean log = new LogOrderBean();
			log.setTelPhone(telPhone);
			log.setPort(port);
			log.setCommand(command);
			log.setConsume(consume);
			logOrderDao.insertTempOrder(log);
			MobileUserBean user = mobileUserDao.findByTelPhone(telPhone);//获取该用户信息
			if(user!=null){//用户存在
				logger.info("有用户存在，进行消费记录处理!");
				return dealOrder(port, command, consume, user);
			}else{//用户不存在，插入临时订单表
				logger.info("用户不存在，消费记录移到临时表!");
				TempOrderBean order = new TempOrderBean();
				order.setTelPhone(telPhone);
				order.setPort(port);
				order.setCommand(command);
				order.setConsume(consume);
				int count = tempOrderDao.insertTempOrder(order);
				return count==1?true:false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
			return false;
		}
	}

	/**
	 * 处理订单
	 * @param port
	 * @param command
	 * @param consume
	 * @param user
	 * @return
	 * @throws SQLException
	 */
	public boolean dealOrder(String port, String command, int consume, MobileUserBean user) throws SQLException {
		int provinceId = user.getProvinceId();
		int userId = user.getUserId();
		SpBean sp = spDao.findSpByPortAndComand(port, command, provinceId);//获取该计费的SP
		if(sp!=null){
			int spId = sp.getSpId();
			BillingSpBean billingSp = billingSpDao.findBySpId(spId);
			int billingId = billingSp.getBillingId();
			BillingBean billing = billingDao.findBillingById(billingId);//获取计费点
			int consumeType = billing.getBillingType();//获取订单类型
			int productId = billing.getProductId();
			ConsumeOrderBean order = consumeOrderDao.findByCondition(userId, billingId, consumeType);//获取该用户该计费点的消费记录
			if(order!=null){//如果有记录，则表示用户当月已消费过
				logger.info("当月有消费记录，更新用户消费记录,OrderId="+order.getOrderId());
				int count = consumeOrderDao.updateConsume(order.getOrderId(), consume);
				return count==1?true:false;
			}else{//如果没有记录，则表示用户当月未消费过
				logger.info("当月没有消费记录，新增用户消费记录");
				order = new ConsumeOrderBean();
				order.setUserId(userId);
				order.setProductId(productId);
				order.setMoblieTypeId(user.getCid());
				order.setConsume(consume);
				order.setBillingId(billingId);
				order.setStatus(0);
				order.setConsumeType(consumeType);
				int count = consumeOrderDao.insertOrder(order);
				return count==1?true:false;
			}
		}else{
			logger.error("SP为空，处理失败！");
			return false;
		}
	}
	
	public boolean delTempOrder(int tempId){
		tempOrderDao.delTempOrderById(tempId);
		return true;
	}
	
	/**
	 * 查询该用户某计费点的消费记录
	 * @param userId
	 * @param billingId
	 * @param consumeType
	 * @return
	 */
	public ConsumeOrderBean findByCondition(int userId,int billingId,int consumeType){
		try {
			return consumeOrderDao.findByCondition(userId, billingId, consumeType);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 查询总数
	 * @param telPhone
	 * @param status
	 * @return
	 */
	public int findCount(String telPhone,int status){
		return consumeOrderDao.findCount(telPhone, status);
	}
	
	/**
	 * 根据条件分页查询
	 * @param telPhone
	 * @param status
	 * @param start
	 * @param pageSize
	 * @return
	 * @throws SQLException
	 */
	public List<ConsumeOrderBean> findByCondition(String telPhone,int status ,int start,int pageSize){
		try {
			return consumeOrderDao.findByCondition(telPhone, status, start, pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
}
